package com.four.project.service.impl;

import com.four.project.pojo.CustomeReq;
import com.four.project.service.CustomeService;
import com.four.project.dao.CustomeRepository;
import com.four.project.pojo.CustomeResp;
import com.four.project.pojo.Customer;
import com.four.project.utils.MD5Util;
import com.four.project.utils.RandomUtil;
import org.apache.catalina.User;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.mail.javamail.JavaMailSender;
import org.springframework.mail.javamail.MimeMessageHelper;
import org.springframework.stereotype.Service;
import org.springframework.web.servlet.handler.UserRoleAuthorizationInterceptor;
import sun.security.provider.MD5;

import javax.mail.MessagingException;
import javax.mail.internet.MimeMessage;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

/**
 * Created by 54110 on 2020/7/1.
 */
@Service
public class CustomeServiceImpl implements CustomeService {

    @Autowired
    CustomeRepository customeRepository;

    @Override
    public String insert(Customer customer) {
        //加密
        String pw = MD5Util.toMD5(customer.getPassword());
        customer.setPassword(pw);
        Customer save = customeRepository.save(customer);
        if (save!=null){
            return "success";
        }
        return "fail";
    }

    @Override
    public CustomeResp findAll(Integer page, Integer size) {
        //分页
        PageRequest pageRequest = new PageRequest(page - 1, size);

        Page<Customer> all = customeRepository.findAll(pageRequest);

        CustomeResp cus = new CustomeResp();
        //返回数据封装到对象中
        cus.setTotal(all.getTotalElements());
        cus.setList(all.getContent());

        return cus;
    }

    @Override
    public Customer updateCustom(Customer customer) {
        //加密
        String pw = MD5Util.toMD5(customer.getPassword());
        customer.setPassword(pw);
        return customeRepository.saveAndFlush(customer);
    }

    @Override
    public void deleteById(Integer id) {
        customeRepository.deleteById(id);
    }

    /**
     * 发送验证码邮件
     * @param email
     * @param request
     */
    @Autowired
    JavaMailSender javaMailSender;
    @Value("${spring.mail.username}")
    private String from;
    @Override
    public void sendMail(String email, HttpServletRequest request) {
        //创建邮件对象
        MimeMessage mimeMessage = javaMailSender.createMimeMessage();
        try {
            //将邮件对象放入mimeMessageHelper(消息代理对象)
            MimeMessageHelper mimeMessageHelper = new MimeMessageHelper(mimeMessage,true);
            //发件人
            mimeMessageHelper.setFrom(from);
            //收件人
            mimeMessageHelper.setTo(email);
            //题目
            mimeMessageHelper.setSubject("测试发送验证邮件");
            //主体内容
            String num = RandomUtil.randomInteger();
            HttpSession session = request.getSession();
            session.setAttribute(email,num);
            mimeMessageHelper.setText("<font stule='color:red'>"+num+"</font>",true);
            //发送
            javaMailSender.send(mimeMessage);
        }catch (MessagingException e) {
            e.printStackTrace();
        }
    }

    /**
     * 用户登录验证
     * @param customeReq
     * @param request
     * @return
     */
    @Override
    public String login(CustomeReq customeReq, HttpServletRequest request) {
        String email = customeReq.getEmail();
        Customer customer = customeRepository.findByEmail(email);
        if (customer==null){
            return "fail";
        }
        String password = customer.getPassword();
        //Md5加密
//        String MD5password = MD5Util.toMD5(customer.getPassword());
        if (!customeReq.getPassword().equals(password)){
            return "success";
        }
        HttpSession session = request.getSession();
        session.setAttribute("customer",customer);
        return "success";
    }

}
